<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------


// 应用公共文件
/**
 * 生成订单号
 * @author hejianyu
 */
function orderNum()
{
    // return  chr(rand(65, 90)) . date('YmdHis', time()) . numberCaptcha(4);
    return  chr(rand(65, 90)) . date('YmdHis', time()).chr(rand(65, 90));
}


/**
 * @param $url 网址
 * @param $filename 保存文件名
 * @param int $timeout 过期时间
 * @return bool
 * 下载远程文件
 */
function http_down($url, $filename, $timeout = 60)
{
    $path = dirname($filename);
    if (!is_dir($path) && !mkdir($path, 0755, true)) {
        return false;
    }
    $fp = fopen($filename, 'w');
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    return $filename;
}



/**
 * API 返回JSON数据
 * @param string $code
 * @param string $message
 * @param array $data
 */
function apiResponse($code='1',$message='',$data=array()){
    header('Access-Control-Allow-Origin:*');
    header('Content-Type:application/json;charset=utf-8');
    $result=array(
      'code'=>$code,
      'message'=>$message,
      'data'=>$data,
    );
    die(json_encode($result,JSON_UNESCAPED_UNICODE));
}

